package com.vvitt.kotlinhiltcarhome.adapter

import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.recyclerview.widget.RecyclerView
import com.vvitt.kotlinhiltcarhome.databinding.ItemNetworkStateBinding

/**
 * @ClassName NetWorkStateItemViewHolder
 * @author please call me police uncel
 * @since 2023/5/26.
 * @email 110
 * @Version: V1.0.0
 * @desciption NetWorkStateItemViewHolder
 **/
class NetWorkStateItemViewHolder(
    private val binding: ItemNetworkStateBinding,
    val retryCallback: () -> Unit
) : RecyclerView.ViewHolder(binding.root){

    fun bindData(data: LoadState){
        binding.apply {
            // 正在加载，显示进度条
            progress.isVisible = data is LoadState.Loading
            // 加载失败，显示并点击重试按钮
            retryButton.isVisible = data is LoadState.Error
            retryButton.setOnClickListener { retryCallback() }
            // 加载失败显示错误原因
            errorMsg.isVisible = !(data as? LoadState.Error)?.error?.message.isNullOrBlank()
            errorMsg.text = (data as? LoadState.Error)?.error?.message
        }
    }
}